﻿// TestCaseGeneratorUI
// (c)2007 Mark Bulmahn. mBulmahn@web.de
// License: GPL 2.0
// Project: TestCaseGenerator. http://www.codeplex.com/TestCaseGenerator

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TestCaseModel;

namespace TestCaseGenerator
{
    public partial class FrmRuleExpressionEditor : Form
    {
        ResultRule oldRule = new ResultRule();

        ResultRule rule = null;
        public ResultRule Rule
        {
            get { return rule; }
            set 
            { 
                rule = value;
            }
        }

        TestModel model = null;

        public TestModel Model
        {
            get { return model; }
            set { model = value; }
        }



        public FrmRuleExpressionEditor()
        {
            InitializeComponent();
            parameterClassesControl1.SelectionOnly = true;
        }

        public void Resync()
        {
            parameterClassesControl1.TestParameters = model.TestParameters;
            textCondition.Text = rule.RuleExpression;
            cboResult.Text = rule.ResultName;
            chkResult.Checked = rule.ResultState;
            textRuleName.Text = rule.RuleName;
            chkObsolet.Checked = rule.ObsoleteTestCase;

            foreach (ExpectedResult res in model.TestResults)
                cboResult.Items.Add(res.Id);

            oldRule.ResultName = rule.ResultName;
            oldRule.ResultState = rule.ResultState;
            oldRule.RuleExpression = rule.RuleExpression;
            oldRule.RuleName = rule.RuleName;
            oldRule.ObsoleteTestCase = rule.ObsoleteTestCase;

            // show the result area only if the rule is not a null condition rule
            if (rule.RuleType == ResultRule.ResultRuleType.ResultRule)
            {
                groupResult.Visible = true;
                groupResult.Enabled = true;
                textRuleName.Enabled = true;
                Text = rule.RuleName + " - Result rule";
            }
            else
            {
                groupResult.Visible = false;
                groupResult.Enabled = false;
                textRuleName.Enabled = false;
                Text = rule.RuleName + " - Null condition";
            }
        }

        private void InitializeGrid()
        {
        }

        private void parameterClassesControl1_DoubleClick(Parameter currentParameter, EquivClass currentClass)
        {
            if (currentClass == null)
            {
                textCondition.Text += "[" + currentParameter.Name + "]";
            }
            else
            {
                textCondition.Text += "'" + currentClass.Id + "'";
            }
            textCondition.Focus();
            textCondition.SelectionStart = textCondition.Text.Length;
        }

        private void butOK_Click(object sender, EventArgs e)
        {
            rule.RuleExpression = textCondition.Text;
            rule.RuleName = textRuleName.Text;
            rule.ResultName = cboResult.Text;
            rule.ResultState = chkResult.Checked;
            rule.ObsoleteTestCase = chkObsolet.Checked;
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            rule.ResultName = oldRule.ResultName;
            rule.ResultState = oldRule.ResultState;
            rule.RuleExpression = oldRule.RuleExpression;
            rule.RuleName = oldRule.RuleName;
        }

        private void OperatorButtonEventHandler(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            textCondition.Text += " " + b.Text + " ";
            textCondition.Focus();
            textCondition.SelectionStart = textCondition.Text.Length;
        }        
    }
}
